package com.one.efaimaly.main.ui.fragment;

import android.widget.RadioGroup;

import com.imnjh.imagepicker.util.LogUtils;
import com.one.common.config.OrderState;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.event.Subscribe;
import com.one.common.model.event.CommonEvent;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.utils.map.LatLng;
import com.one.common.view.pagestate.refreshlayout.utils.LogUtil;
import com.one.efaimaly.utils.LocationUtils;
import com.one.common.utils.map.MapUtils;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListFragment;
import com.one.efaimaly.R;
import com.one.efaimaly.main.model.item.OrderEmptyItem;
import com.one.efaimaly.main.model.item.OrderItem;
import com.one.efaimaly.main.presenter.MainPresenter;
import com.one.efaimaly.main.ui.binder.OrderBinder;
import com.one.efaimaly.main.ui.binder.OrderEmptyBinder;
import com.one.efaimaly.order.model.extra.UploadImgExtra;
import com.tencent.map.geolocation.TencentLocation;

import static com.one.common.config.OrderState.PAY_END;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 上午9:21
 * description ：我的订单
 **/
public class OrderFragment extends BaseListFragment<MainPresenter> implements IListView,
        RadioGroup.OnCheckedChangeListener,
        OrderBinder.OrderBtnClickListener,
        LocationUtils.OnLocationListener{
    private String orderState = OrderState.ALL;
    private RadioGroup rgOrderState;
    private LatLng meLatlng;
    private String starName;
    @Override
    protected void initPresenter() {
        mPresenter = new MainPresenter(this, getActivity());
    }

    @Override
    public void registerDate() {
        register(OrderItem.class, new OrderBinder(this));
        register(OrderEmptyItem.class, new OrderEmptyBinder());
    }

    @Override
    public void loadData() {
        mPresenter.getOrderList(getPage() + "", orderState,false);
    }

    @Override
    public boolean onEmpty() {
        addData(new OrderEmptyItem());
        return true;
    }

    @Override
    public void initView() {
        super.initView();
        addTopView(R.layout.layout_order_top);

        rgOrderState = successView.findViewById(R.id.rg_order_state);
        rgOrderState.setOnCheckedChangeListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        LocationUtils.location(getActivity(), this);

    }

    @Override
    public void onResume() {
        super.onResume();
        doRefresh();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_all:
                orderState = OrderState.ALL;
                break;
            case R.id.rb_visit:
                orderState = OrderState.WAIT_VISIT;
                break;
            case R.id.rb_confirm:
                orderState = OrderState.WAIT_CONFIRM;
                break;
            case R.id.rb_finish:
                orderState = OrderState.PAY_END;
                break;
            case R.id.rb_end:
                orderState = OrderState.CANCEL;
                break;
            default:
                break;
        }

        doRefresh();
    }

    @Override
    public void onNavigation(OrderItem item) {
        if (item != null && item.getMaintailOrderAddressBean() != null) {
            MapUtils.selectMap(getActivity(), meLatlng, starName,
                    item.getMaintailOrderAddressBean().getLatLng(),
                    item.getMaintailOrderAddressBean().getAddress());
        }
    }

    @Override
    public void onCancel(OrderItem item) {
        if(item.getOrder_state().equals(PAY_END)){
            if(StringUtils.isBlank(item.getOrder_maintail_imgs1()) &&
                    StringUtils.isBlank(item.getOrder_maintail_imgs2())){
                // 上传图片
                RouterManager.getInstance().go(RouterPath.UPLOAD_IMG, new UploadImgExtra(item.getOrder_id()+""));
            } else {
                // 查看图片
                RouterManager.getInstance().go(RouterPath.UPLOAD_IMG,
                        new UploadImgExtra(item.getOrder_maintail_imgs1(),
                                item.getOrder_maintail_imgs2(),item.getOrder_id()+""));
            }
        } else { // 取消订单
            mPresenter.cancel(item.getOrder_id()+"");
        }


    }

    @Override
    public void onLocated(TencentLocation aMapLocation) {
        if(aMapLocation!=null && StringUtils.isNotBlank(aMapLocation.getCity())){
            starName = aMapLocation.getAddress();
            meLatlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
        }
    }

    @Override
    public void onLocationPermissionFail() {
        Toaster.showLongToast(StringUtils.format(R.string.permission_fail_local), MyToast.FAIL);
    }

    /**
     * 收到推送更新列表
     */
    @Subscribe
    public void onJpush(CommonEvent event){
        if(event!=null){
            doRefresh();
        }
    }
}
